﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MainScene;
using MindFusion.Diagramming.WinForms;
using MindFusion.Diagramming;

namespace SSShell
{
    public partial class AddChangeNode : Form
    {
        frmMain formMain;
        ClassNode Selected_Node;
        DiagramNode Selected_DNode;
        Boolean is_redacted = false;
        TypeNode old_type;
        float X, Y;
        public AddChangeNode(frmMain _formMain, Boolean redacted, ClassNode _selnode, DiagramNode _selDNode, float x, float y)
        {
            
            InitializeComponent();
            formMain = _formMain;
            is_redacted = redacted;
            Selected_Node = _selnode;
            Selected_DNode = _selDNode;
            if (Selected_Node!=null)
            Y = y; 
            X = x;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (comboBox1.SelectedIndex == 0)
            {
                groupBox1.Visible = true;
                groupBox2.Visible = false;
            }
            else
            {
                groupBox2.Visible = true;
                groupBox1.Visible = false;
                cmbAgent.Items.Clear();
                cmbRec.Items.Clear();
                foreach (string item in formMain.SS.LstAgents)
                {
                    cmbAgent.Items.Add(item);
                }
                foreach (string item in formMain.SS.LstRecipients)
                {
                    cmbRec.Items.Add(item);
                }
                foreach (string item in formMain.SS.LstLocotivs)
                {
                    cmbLoc.Items.Add(item);
                }
                foreach (string item in formMain.SS.LstObjs)
                {
                    cmbObj.Items.Add(item);
                }
                foreach (string item in formMain.SS.LstTimes)
                {
                    cmbTime.Items.Add(item);
                }
            }
        }

        private void AddChangeNode_Load(object sender, EventArgs e)
        {
            if (!is_redacted)
                comboBox1.SelectedIndex = 0;
            else
            {
                if (Selected_Node.TypeN == TypeNode.Action)
                {
                    comboBox1.SelectedIndex = 1;
                    cmbAgent.Text = Selected_Node.CaseFrame.Agent.Name;
                    cmbRec.Text = Selected_Node.CaseFrame.Recipient.Name;
                    txtAction.Text = Selected_Node.CaseFrame.ToDo.Name;
                    cmbLoc.Text = Selected_Node.CaseFrame.Lokotiv.Name;
                    cmbTime.Text = Selected_Node.CaseFrame.Time.Name;
                    cmbObj.Text = Selected_Node.CaseFrame.Obj.Name;

                }
                else
                {
                    comboBox1.SelectedIndex = 0;
                    txtScene.Text = Selected_Node.Name;
                }


            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (!is_redacted)
            {
                if (comboBox1.SelectedIndex == 0)
                {
                    formMain.SS.AddScene(txtScene.Text);
                }
                if (comboBox1.SelectedIndex == 1)
                    formMain.SS.AddAction(cmbAgent.Text, cmbRec.Text, txtAction.Text, cmbLoc.Text, cmbTime.Text, cmbObj.Text, X, Y);
            }
            else
            {
                if (comboBox1.SelectedIndex == 0)
                {
                    if (Selected_Node.TypeN != TypeNode.Scene)
                    {
                        
                        IconNode newn = (IconNode)Selected_DNode;
                        newn.ColOfNode = Color.Red;
                        formMain.diagramView1.Refresh();
                        Selected_Node.TypeN = TypeNode.Scene;
                        //start Ирина (22.03.11)
                        ShapeNode Shnode = formMain.GetShapeNodeById((ClassNode)newn.Id);
                        DiagramLinkCollection allLink = Shnode.GetAllLinks();
                        foreach (DiagramLink item in allLink)
                        {
                            formMain.diagram2.Nodes.Remove(item.Destination);
                            formMain.diagram2.Links.Remove(item);
                        }
                        Shnode.Text = txtScene.Text;
                        //end Ирина (22.03.11)
                    }
                    formMain.SS.ChScene(Selected_Node, txtScene.Text);
                    //start Ирина (22.03.11)
                    ShapeNode Shnode1 = formMain.GetShapeNodeById((ClassNode)((IconNode)Selected_DNode).Id);
                    Shnode1.Text = txtScene.Text;
                    //end Ирина (22.03.11)
                }
                if (comboBox1.SelectedIndex == 1)
                {
                    if (Selected_Node.TypeN != TypeNode.Action)
                    {
                        if (Selected_Node.LstNodes.Count != 0)
                        {
                            DialogResult ToContineu = MessageBox.Show("Сцена ссылалась на другие вершины, в случае изменения они будут удалены", "Внимание!",
                           MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                            if (ToContineu == DialogResult.Yes)
                            {
                                List<DiagramLink> links_to_remove = new List<DiagramLink>();

                                foreach (DiagramLink item in formMain.diagram1.Links)
                                {
                                    if (item.Origin.Id == Selected_Node)
                                        links_to_remove.Add(item);
                                }
                                Selected_Node.LstNodes.Clear();
                                foreach (DiagramLink item in links_to_remove)
                                {
                                    formMain.diagram1.Links.Remove(item);
                                }
                                links_to_remove.Clear();
                            }
                            else
                            {
                                return;
                            }

                        }
                        IconNode newnn = (IconNode)Selected_DNode;
                        newnn.ColOfNode = Color.Green;
                        formMain.diagramView1.Refresh();
                        //start Ирина (22.03.11)
                        Selected_Node.Name = "";
                        Change_to_LookDiagram(Selected_Node);
                        formMain.diagramView2.Refresh();
                        //end Ирина (22.03.11)

                        Selected_Node.TypeN = TypeNode.Action;
                        Selected_Node.CaseFrame = new ClassCaseFrame();
                        formMain.SS.ChAction(Selected_Node, cmbAgent.Text, cmbRec.Text, txtAction.Text, cmbLoc.Text, cmbTime.Text, cmbObj.Text);
                       
                        //start Ирина (22.03.11)
                        Selected_Node.CaseFrame.Agent.X_d2 = X - 30;
                        Selected_Node.CaseFrame.Agent.Y_d2 = Y;
                        Selected_Node.CaseFrame.Recipient.X_d2 = X - 30;
                        Selected_Node.CaseFrame.Recipient.Y_d2 = Y - 5;
                        Selected_Node.CaseFrame.ToDo.X_d2 = X - 30;
                        Selected_Node.CaseFrame.ToDo.Y_d2 = Y - 10;
                        Selected_Node.CaseFrame.Lokotiv.X_d2 = X + 30;
                        Selected_Node.CaseFrame.Lokotiv.Y_d2 = Y;
                        Selected_Node.CaseFrame.Obj.X_d2 = X + 30;
                        Selected_Node.CaseFrame.Obj.Y_d2 = Y - 5;
                        Selected_Node.CaseFrame.Time.X_d2 = X + 30;
                        Selected_Node.CaseFrame.Time.Y_d2 = Y - 10;
                        //end Ирина (22.03.11)

                    }
                    else
                    {

                        formMain.SS.ChAction(Selected_Node, cmbAgent.Text, cmbRec.Text, txtAction.Text, cmbLoc.Text, cmbTime.Text, cmbObj.Text);
                    }
                    //start Ирина (22.03.11)
                    ShapeNode sh_item = formMain.GetShapeNodeById(Selected_Node);
                    ClassCase item_case;
                    foreach (DiagramLink link in sh_item.GetAllLinks())
                    {
                        try
                        {
                            item_case = (ClassCase)link.Destination.Id;
                            ((ShapeNode)link.Destination).Text = item_case.Name;
                        }
                        catch (InvalidCastException)
                        {
                        }
                    }
                    //end Ирина (22.03.11)
                }

                MessageBox.Show("Вершина успешно редактирована");
            }
            //добавление вершины
            this.Close();
        }
        //start Ирина (22.03.11)
        public void Change_to_LookDiagram(ClassNode node)
        {
            ShapeNode item_case;
            DiagramLink link;
            ShapeNode item = formMain.GetShapeNodeById(node);
            List<ClassCase> allcase = new List<ClassCase>() { node.CaseFrame.Agent, node.CaseFrame.Recipient, 
                    node.CaseFrame.ToDo, node.CaseFrame.Lokotiv, node.CaseFrame.Obj, node.CaseFrame.Time};
            List<string> allcaseName = new List<string>() { "#агент", "#реципиент", "#действие", "#локотив",
                    "#объект", "#время"};
            int i = 0;
            item.Text = "";
            foreach (ClassCase clcase in allcase)
            {
                if (clcase.Name.Trim() != "")
                {
                    item_case = new ShapeNode(formMain.diagram2);
                    item_case.Id = clcase;
                    item_case.Bounds = new RectangleF(clcase.X_d2, clcase.Y_d2, 10, 5);
                    item_case.Shape = Shapes.Ellipse;
                    item_case.Text = clcase.Name.Trim();
                    item_case.Brush = new MindFusion.Drawing.SolidBrush(Color.CadetBlue);
                    item_case.ShadowOffsetX = item_case.ShadowOffsetY = 0;
                    formMain.diagram2.Nodes.Add(item_case);
                    link = new DiagramLink(formMain.diagram2, item, item_case);
                    link.Text = allcaseName.ElementAt<string>(i);
                    link.AllowMoveEnd = link.AllowMoveStart = false;
                    link.ShadowOffsetX = link.ShadowOffsetY = 0;
                    formMain.diagram2.Links.Add(link);
                    i++;
                }
            }
        }
        //end Ирина (22.03.11)
    }
}
